package com.xmg.p2p.base.service.impl;

import com.xmg.p2p.base.domain.Account;
import com.xmg.p2p.base.mapper.AccountMapper;
import com.xmg.p2p.base.service.IAccountService;
import com.xmg.p2p.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * lcd  2020/5/8
 * Description:处理乐观锁问题先写出来,其他地方必须调用service不能调用dao
 */
@Service
public class AccountServiceImpl implements IAccountService {
    @Autowired
    private AccountMapper accountMapper;
    @Override
    public void update(Account account) {
        int i = accountMapper.updateByPrimaryKey(account);
        if (i == 0) {
            throw new RuntimeException("乐观锁错误,Account:" + account.getId());
        }
    }

    @Override
    public void add(Account account) {
        accountMapper.insert(account);
    }

    @Override
    public Account findById(Long id) {
        return accountMapper.selectByPrimaryKey(id);
    }

    @Override
    public Account getCurrent() {
        return accountMapper.selectByPrimaryKey(UserContext.getLogininfo().getId());
    }
}
